
$(function () {
    $("#meeting-join .btn-save").click(function () {
        var name = $("input[name='join-name']").val();
        var meetingPassword = $("#meeting-join").find("input[name='meetingPassword']").val();
        var moderatorPassword = $("#meeting-join").find("input[name='moderatorPassword']").val();
        var isModerator = $("input[name='isModerator']").prop('checked');

        if(name.length <= 0) {
            $MB.n_danger("请输入会议名称");
            return;
        }

        if(meetingPassword.length >0 && meetingPassword.length < 6) {
            $MB.n_danger("请输入6位会议密码");
            return;
        }

        if(isModerator && moderatorPassword.length < 6) {
            $MB.n_danger("请输入6位主持人密码");
            return;
        }

        var data = {
                        "name" : name,
                        "meetingPassword" : meetingPassword,
                        "moderatorPassword" : moderatorPassword,
                        "isModerator" : $("input[name='isModerator']").prop('checked')
                    };

        $.ajax({
            type : "post",
            url : "/meeting/join/" + $("input[name='meetingJoinId']").val(),
            cache : false,
            data : data,
            async: false,
            success : function(r) {
                if(r.code == 0) {
                    window.open(r.msg.url, '_blank').location;
                } else {
                    if(r.code == 609) {//locked
                        showMeetingLock(meetingId, 'user-' + userId, username);
                    } else {
                        $MB.n_danger($MB.i18n(r.msg));
                    }
                }
            }
        });
    });

    $("#meeting-join .btn-close").click(function () {
        closeJoinModal();
    });
});

function joinMeeting() {
    var selected = $("#meetingTable").bootstrapTable('getSelections');
    var selected_length = selected.length;

    if (!selected_length) {
        $MB.n_warning('请勾选需要加入的会议！');
        return;
    }
    if (selected_length > 1) {
        $MB.n_warning('一次只能加入一个会议！');
        return;
    }

    var name = $("#meeting-join").find("#meeting-join-name");
    name.empty();
    name.append(selected[0].name);

    $('#meeting-join').find("input[name='meetingJoinId']").val(selected[0].id);
    $('#meeting-join').modal();
}

function closeJoinModal() {
    $MB.closeAndRestModal("meeting-join");
}